﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.DailyPrayers
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.DailyPrayers.Index.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.DailyPrayers.Index.PageLoad();
            

            using (DataSet ds = DB.RunDataSet(@"
SELECT * FROM DailyPrayers.Locations
", CommandType.Text))
            {
                data.Locations = new Shared.Entities.DailyPrayers.LocationCollection();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var location = new Shared.Entities.DailyPrayers.Location();
                    location.ID = dr["LocationID"].DbInt();
                    location.IsMaster = dr["IsMaster"].DbBool();
                    location.InheritMaster = dr["InheritMaster"].DbBool();
                    location.Name = dr["Name"].DbString();
                    location.Street = dr["Street"].DbString();
                    location.Street2 = dr["Street2"].DbString();
                    location.City = dr["City"].DbString();
                    location.State = dr["State"].DbString();
                    location.ZipCode = dr["ZipCode"].DbString();
                    location.LongNote = dr["LongNote"].DbString();
                    location.Latitude = dr["Latitude"].DbDecimal();
                    location.Longitude = dr["Longitude"].DbDecimal();
                    location.DisplayOrder = dr["DisplayOrder"].DbInt();
                    data.Locations.Add(location);
                }
            }

            return data;
        }

        public static void DeleteLocation(int id)
        {
            DB.Run(@"
DELETE DailyPrayers.PrayerTimes
WHERE LocationID = @LocationID

DELETE DailyPrayers.Locations
WHERE LocationID = @LocationID
", CommandType.Text,
                    DB.CreateParameter("@LocationID", id, SqlDbType.Int));
        }
    }
}
